/*-----------------------------------------------------
 * 权限子系统-系统历史记录模块
 * ---------------------------------------------------*/
jx.ready(function () {
    window.api = {
        grid: '/auth/history/grid',
        details: '/auth/history/details',
        export: '/auth/history/export'
    }
    //定义变量
    var $grid = $('#grid'),$timeButton = $('#time-horizon > a');
    var gridInstance;
    var gridUrl = jx.apiUrl(api.grid),detailsUrl = jx.apiUrl(api.details),
        exportUrl = jx.apiUrl(api.export);

    var setTimeHorizon = function ($this) {
        $timeButton.removeClass('active');
        $this.addClass('active');
        var $start = $('#operateStartDateTime');
        var $end = $('#operateEndDateTime');
        var cdate = jx.formatDate(new Date());
        switch ($this.attr('data-value')) {
            case '1'://今天
                $start.val(cdate);
                $end.val(cdate);
                break;
            case '2'://近7天
                $start.val(jx.formatDate(jx.addDate(new Date(), -7)));
                $end.val(cdate);
                break;
            case '3'://近一个月
                $start.val(jx.formatDate(jx.addDate(new Date(), -30)));
                $end.val(cdate);
                break;
            case '4'://近三个月
                $start.val(jx.formatDate(jx.addDate(new Date(), -90)));
                $end.val(cdate);
                break;
            default:
                break;
        }
        $('#gridform').jxform().submit();
    };

    //初始化表格
    var initGrid = function () {
        gridInstance = $grid.jxgrid({
            url: gridUrl,
            onDblClickRow: function (index, row) {
                detailsData(row);
            },
            onBeforeLoad: function (param) {
                if (param.operateEndDateTime && param.operateStartDateTime) {
                    return true;
                }
                return false;
            }
        });
    };

    //初始化事件
    var initEvent = function () {
        $('#btn-advsearch').click(function () {
            if ($('#gridadvform').valid()) {
                $('#gridform').jxform().submit();
                $('#gridadvform-popover').popoverX('hide');
            }
        });

        $timeButton.click(function () {
            setTimeHorizon($(this));
        });

        $('.nav-tabs > li').on('click', function () {
            var $form = $('#gridform');
            $form.find('#operateType').val($(this).data('value'));
            $form.find(':submit').click();
        });

        $('#btn-export').click(function () {
            exportData();
        });
    };

    //查看数据
    var detailsData = function (row) {
        jx.auth.detailsData(gridInstance, row, {
            url: detailsUrl,
            maxmin: true,
            width: '70%'
        });
    };

    //导出数据
    var exportData = function () {
        $(document.body).mask('正在导出数据,请稍等...', 300);
        jx.export(exportUrl, $.extend(jx.serialize($('#gridform')),jx.serialize($('#gridadvform'))));
        // jx.export(exportUrl, $.extend(jx.serialize($gridform),jx.serialize($('#griddateform')),jx.serialize($('#gridadvform'))));
        $(document.body).unmask();
    };

    //定义表格列格式化函数
    jx.gf.type = function (v, row, index) {
        var cls = 'default';
        if (v == '1') {
            cls = 'success';
        }
        else if (v == '2') {
            cls = 'primary';
        }
        else if (v == '3') {
            cls = 'danger';
        }
        return '<span class="label label-' + cls + '">' + row.operateTypeText + '</span>';
    };

    //初始化
    initGrid();
    initEvent();
    $timeButton.eq(1).click();
});